CMAKE_MINIMUM_REQUIRED(VERSION 3.8)

# FastDeploy basic infos
set(FASTDEPLOY_VERSION @FASTDEPLOY_VERSION@)
set(LIBRARY_NAME @LIBRARY_NAME@)

# If compile with GLIBC_CXX_ABI=0
set(NEED_ABI0 @NEED_ABI0@)

# Hardware and Language API
set(WITH_GPU @WITH_GPU@)
set(WITH_IPU @WITH_IPU@)
set(WITH_OPENCL @WITH_OPENCL@)
set(WITH_ASCEND @WITH_ASCEND@)
set(WITH_DIRECTML @WITH_DIRECTML@)
set(WITH_TIMVX @WITH_TIMVX@)
set(WITH_KUNLUNXIN @WITH_KUNLUNXIN@)
set(WITH_CAPI @WITH_CAPI@)
set(WITH_CSHARPAPI @WITH_CSHARPAPI@)
set(WITH_TESTING @WITH_TESTING@)
set(BUILD_ON_JETSON @BUILD_ON_JETSON@)
set(RKNN2_TARGET_SOC "@RKNN2_TARGET_SOC@")

# Inference backend and FastDeploy Moudle
set(ENABLE_ORT_BACKEND @ENABLE_ORT_BACKEND@)
set(ENABLE_RKNPU2_BACKEND @ENABLE_RKNPU2_BACKEND@)
set(ENABLE_TVM_BACKEND @ENABLE_TVM_BACKEND@)
set(ENABLE_HORIZON_BACKEND @ENABLE_HORIZON_BACKEND@)
set(ENABLE_SOPHGO_BACKEND @ENABLE_SOPHGO_BACKEND@)
set(ENABLE_LITE_BACKEND @ENABLE_LITE_BACKEND@)
set(ENABLE_PADDLE_BACKEND @ENABLE_PADDLE_BACKEND@)
set(ENABLE_OPENVINO_BACKEND @ENABLE_OPENVINO_BACKEND@)
set(ENABLE_POROS_BACKEND @ENABLE_POROS_BACKEND@)
set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@)
set(ENABLE_PADDLE2ONNX @ENABLE_PADDLE2ONNX@)
set(BUILD_PADDLE2ONNX @BUILD_PADDLE2ONNX@)

set(ENABLE_VISION @ENABLE_VISION@)
set(ENABLE_FLYCV @ENABLE_FLYCV@)
set(ENABLE_CVCUDA @ENABLE_CVCUDA@)
set(ENABLE_TEXT @ENABLE_TEXT@)
set(ENABLE_ENCRYPTION @ENABLE_ENCRYPTION@)
set(ENABLE_BENCHMARK @ENABLE_BENCHMARK@)

# Version infos and custom settings for third libs
set(PADDLEINFERENCE_VERSION @PADDLEINFERENCE_VERSION@)
set(POROS_VERSION @POROS_VERSION@)
set(OPENVINO_VERSION @OPENVINO_VERSION@)
set(OPENCV_FILENAME @OPENCV_FILENAME@)
set(OPENVINO_FILENAME @OPENVINO_FILENAME@)
set(PADDLELITE_FILENAME @PADDLELITE_FILENAME@)
set(OPENCV_DIRECTORY "@OPENCV_DIRECTORY@")
set(ORT_DIRECTORY "@ORT_DIRECTORY@")
set(OPENVINO_DIRECTORY "@OPENVINO_DIRECTORY@")

# Android: specific option for Android OS
set(WITH_ANDROID_STATIC_LIB @WITH_ANDROID_STATIC_LIB@)
set(WITH_ANDROID_LITE_STATIC @WITH_ANDROID_LITE_STATIC@)
set(WITH_ANDROID_OPENCV_STATIC @WITH_ANDROID_OPENCV_STATIC@)
set(WITH_ANDROID_FLYCV_STATIC @WITH_ANDROID_FLYCV_STATIC@)
set(WITH_ANDROID_OPENMP @WITH_ANDROID_OPENMP@)
set(WITH_ANDROID_JAVA @WITH_ANDROID_JAVA@)
set(WITH_ANDROID_TENSOR_FUNCS @WITH_ANDROID_TENSOR_FUNCS@)

# encryption and auth
set(PADDLEINFERENCE_WITH_ENCRYPT @PADDLEINFERENCE_WITH_ENCRYPT@)
set(PADDLEINFERENCE_WITH_AUTH @PADDLEINFERENCE_WITH_AUTH@)

set(FASTDEPLOY_LIBS "")
set(FASTDEPLOY_INCS "")
list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/include)

# Note(zhoushunjie): include some useful utils function
include(${CMAKE_CURRENT_LIST_DIR}/utils.cmake)

# Set C++11 as standard for the whole project
if(NOT MSVC)
  set(CMAKE_CXX_STANDARD 11)
  set(CMAKE_CXX_FLAGS "-Wno-format")
  if(NEED_ABI0)
    add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
  else()
    add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
  endif()
endif(NOT MSVC)

# Set FastDeploy static lib definitions
if(WITH_ANDROID_LITE_STATIC)
  add_definitions(-DWITH_LITE_STATIC)
  add_definitions(-DWITH_ANDROID_LITE_STATIC)
endif()

if(WITH_ANDROID_STATIC_LIB)
  add_definitions(-DWITH_STATIC_LIB)
  add_definitions(-DWITH_ANDROID_STATIC_LIB)
  # add_definitions(-DWITH_STATIC_WARNING)
endif()

# Still need omp while using FastDeploy static lib.
# This is due to the use of openmp for Paddle Lite's
# static library.
if(ANDROID AND WITH_ANDROID_STATIC_LIB AND WITH_ANDROID_LITE_STATIC)
  include(${CMAKE_CURRENT_LIST_DIR}/openmp.cmake)
endif()

if(ANDROID)
  add_library(fastdeploy STATIC IMPORTED GLOBAL)
  if(WITH_ANDROID_STATIC_LIB)
    set_property(TARGET fastdeploy PROPERTY IMPORTED_LOCATION
                 ${CMAKE_CURRENT_LIST_DIR}/lib/${ANDROID_ABI}/lib${LIBRARY_NAME}_static.a)
  else()
    set_property(TARGET fastdeploy PROPERTY IMPORTED_LOCATION
                 ${CMAKE_CURRENT_LIST_DIR}/lib/${ANDROID_ABI}/lib${LIBRARY_NAME}.so)
  endif()
  list(APPEND FASTDEPLOY_LIBS fastdeploy)
  if(WITH_ANDROID_OPENMP AND (NOT WITH_ANDROID_LITE_STATIC))
    add_library(fastdeploy_omp STATIC IMPORTED GLOBAL)
    set_property(TARGET fastdeploy_omp PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/lib/${ANDROID_ABI}/libomp.so)
    list(APPEND FASTDEPLOY_LIBS fastdeploy_omp)
  endif()
else()
  find_library(FDLIB ${LIBRARY_NAME} ${CMAKE_CURRENT_LIST_DIR}/lib NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${FDLIB})
endif()

if(ENABLE_ORT_BACKEND)
  if (ORT_DIRECTORY)
    set(ORT_LIB_PATH ${ORT_DIRECTORY}/lib)
  else()
    set(ORT_LIB_PATH ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/onnxruntime/lib)
  endif()
  message(STATUS "The path of ONNXRuntime is ${ORT_LIB_PATH}.")
  find_library(ORT_LIB onnxruntime ${ORT_LIB_PATH}  NO_DEFAULT_PATH)
  list(APPEND FASTDEPLOY_LIBS ${ORT_LIB})
endif()

if(ENABLE_TVM_BACKEND)
    if(APPLE)
        set(TVM_RUNTIME_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tvm/lib/libtvm_runtime.dylib)
    else()
        set(TVM_RUNTIME_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tvm/lib/libtvm_runtime.so)
    endif()
    list(APPEND FASTDEPLOY_LIBS ${TVM_RUNTIME_LIB})
endif()

if(ENABLE_PADDLE_BACKEND)
  find_library(PADDLE_LIB paddle_inference ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/paddle/lib NO_DEFAULT_PATH)
  if(WIN32)
    set(DNNL_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mkldnn/lib/mkldnn.lib")
    set(IOMP_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mklml/lib/libiomp5md.lib")
  elseif(APPLE)
    message(STATUS "No third parties libs(mkldnn and omp) need to link into paddle_inference on MacOS OSX.")
  else()
    set(DNNL_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mkldnn/lib/libmkldnn.so.0")
    set(IOMP_LIB "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle_inference/third_party/install/mklml/lib/libiomp5.so")
    set(FDMODEL_LIB "${PADDLEINFERENCE_INSTALL_DIR}/third_party/install/fdmodel/lib/libfastdeploy_wenxin.so")
    set(FDMODEL_MODEL_LIB "${PADDLEINFERENCE_INSTALL_DIR}/third_party/install/fdmodel/lib/libfastdeploy_model.so.2.0.0")
    set(FDMODEL_AUTH_LIB "${PADDLEINFERENCE_INSTALL_DIR}/third_party/install/fdmodel/lib/libfastdeploy_auth.so")
    if((EXISTS ${FDMODEL_LIB}) AND (EXISTS ${FDMODEL_MODEL_LIB}))
      set(PADDLEINFERENCE_WITH_ENCRYPT ON CACHE BOOL "" FORCE)
      list(APPEND FASTDEPLOY_LIBS ${FDMODEL_LIB} ${FDMODEL_MODEL_LIB})
    endif()
    if((EXISTS ${FDMODEL_LIB}) AND (EXISTS ${FDMODEL_AUTH_LIB}))
      set(PADDLEINFERENCE_WITH_AUTH ON CACHE BOOL "" FORCE)
      list(APPEND FASTDEPLOY_LIBS ${FDMODEL_AUTH_LIB})
    endif()
    if(PADDLEINFERENCE_WITH_ENCRYPT OR PADDLEINFERENCE_WITH_AUTH)
      if(WITH_KUNLUNXIN)
        list(APPEND FASTDEPLOY_LIBS -lssl -lcrypto)
      endif()
    endif()
  endif()
  list(APPEND FASTDEPLOY_LIBS ${PADDLE_LIB})
  if(EXISTS "${DNNL_LIB}")
    list(APPEND FASTDEPLOY_LIBS ${DNNL_LIB} ${IOMP_LIB})
  endif()
endif()

if(ENABLE_OPENVINO_BACKEND)
  if (OPENVINO_DIRECTORY)
    set(OPENVINO_DIR ${OPENVINO_DIRECTORY})
  else()
    set(OPENVINO_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENVINO_FILENAME}/runtime)
  endif()
  get_openvino_libs(${OPENVINO_DIR})
  message(STATUS "OPENVINO_LIBS = ${OPENVINO_LIBS}")
  list(APPEND FASTDEPLOY_LIBS ${OPENVINO_LIBS})
endif()

if(ENABLE_RKNPU2_BACKEND)
  if(RKNN2_TARGET_SOC STREQUAL "RK356X")
    set(RKNPU2_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/rknpu2_runtime/lib/librknnrt.so)
  elseif (RKNN2_TARGET_SOC STREQUAL "RK3588")
    set(RKNPU2_LIB ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/rknpu2_runtime/lib/librknnrt.so)
  else ()
    message(FATAL_ERROR "RKNN2_TARGET_SOC is not set, ref value: RK356X or RK3588")
  endif()
  message(STATUS "The path of RKNPU2 is ${RKNPU2_LIB}.")
  list(APPEND FASTDEPLOY_LIBS ${RKNPU2_LIB})
endif()

if(ENABLE_HORIZON_BACKEND)
  set(DNN_PATH ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/dnn)
  set(APPSDK_PATH ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/appsdk/appuser/)

  set(DNN_LIB_PATH ${DNN_PATH}/lib)
  set(APPSDK_LIB_PATH ${APPSDK_PATH}/lib/hbbpu)
  set(BPU_libs dnn cnn_intf hbrt_bernoulli_aarch64)

  link_directories(${DNN_LIB_PATH}
                  ${APPSDK_PATH}/lib/hbbpu
                  ${APPSDK_PATH}/lib)

  list(APPEND FASTDEPLOY_LIBS ${BPU_libs})

endif()
if(ENABLE_LITE_BACKEND)
  set(LITE_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${PADDLELITE_FILENAME})
  if(ANDROID)
    if(WITH_ANDROID_LITE_STATIC)
      if(WITH_ANDROID_STATIC_LIB)
        list(APPEND FASTDEPLOY_INCS ${LITE_DIR}/include)
      endif()
    else()
      add_library(paddle_full_api_shared STATIC IMPORTED GLOBAL)
      set_property(TARGET paddle_full_api_shared PROPERTY IMPORTED_LOCATION ${LITE_DIR}/lib/${ANDROID_ABI}/libpaddle_full_api_shared.so)
      list(APPEND FASTDEPLOY_LIBS paddle_full_api_shared)
    endif()
  else()
    # Linux/Mac/Win/...
    find_library(LITE_LIB paddle_full_api_shared ${LITE_DIR}/lib NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_LIBS ${LITE_LIB})
  endif()
endif()

if(ENABLE_POROS_BACKEND)
  find_library(POROS_LIB poros ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/poros/lib NO_DEFAULT_PATH)
  find_library(TORCH_LIB torch ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/torch/lib NO_DEFAULT_PATH)
  set(TORCH_INCLUDE "${CMAKE_CURRENT_LIST_DIR}/third_libs/install/torch/include")
  list(APPEND FASTDEPLOY_LIBS ${POROS_LIB} ${TORCH_LIB})
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/poros/include ${TORCH_INCLUDE})
endif()

if(WITH_GPU)
  if(NOT CUDA_DIRECTORY)
    set(CUDA_DIRECTORY "/usr/local/cuda")
  endif()
  if(WIN32)
    find_library(CUDA_LIB cudart ${CUDA_DIRECTORY}/lib/x64)
    find_library(NVJPEG_LIB nvjpeg ${CUDA_DIRECTORY}/lib/x64)
  else()
    find_library(CUDA_LIB cudart ${CUDA_DIRECTORY}/lib64)
    if(NOT BUILD_ON_JETSON)
      find_library(NVJPEG_LIB nvjpeg ${CUDA_DIRECTORY}/lib64)
    endif()
  endif()
  if(NOT CUDA_LIB)
    message(FATAL_ERROR "[FastDeploy] Cannot find library cudart in ${CUDA_DIRECTORY}, Please define CUDA_DIRECTORY, e.g -DCUDA_DIRECTORY=/path/to/cuda")
  endif()
  list(APPEND FASTDEPLOY_LIBS ${CUDA_LIB} ${NVJPEG_LIB})
  list(APPEND FASTDEPLOY_INCS ${CUDA_DIRECTORY}/include)

  if(ENABLE_TRT_BACKEND)
    if(BUILD_ON_JETSON)
      find_library(TRT_INFER_LIB nvinfer /usr/lib/aarch64-linux-gnu/)
      find_library(TRT_ONNX_LIB nvonnxparser /usr/lib/aarch64-linux-gnu/)
      find_library(TRT_PLUGIN_LIB nvinfer_plugin /usr/lib/aarch64-linux-gnu/)
    else()
      if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/)
        find_library(TRT_INFER_LIB nvinfer ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
        find_library(TRT_ONNX_LIB nvonnxparser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
        find_library(TRT_PLUGIN_LIB nvinfer_plugin ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib NO_DEFAULT_PATH)
      else()
        find_library(TRT_INFER_LIB nvinfer /usr/lib/x86_64-linux-gnu/)
        find_library(TRT_ONNX_LIB nvonnxparser /usr/lib/x86_64-linux-gnu/)
        find_library(TRT_PLUGIN_LIB nvinfer_plugin /usr/lib/x86_64-linux-gnu/)
      endif()
    endif()
    list(APPEND FASTDEPLOY_LIBS ${TRT_INFER_LIB} ${TRT_ONNX_LIB} ${TRT_PLUGIN_LIB})
  endif()
endif()

if(ENABLE_VISION)
  if(OPENCV_DIRECTORY)
    set(OpenCV_DIR ${OPENCV_DIRECTORY})
  else()
    if(ANDROID)
      set(OpenCV_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENCV_FILENAME}/sdk/native/jni)
      set(OpenCV_NATIVE_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENCV_FILENAME}/sdk/native)
    else()
      set(OpenCV_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/${OPENCV_FILENAME})
      if(WIN32)
        set(OpenCV_DIR ${OpenCV_DIR}/build)
      endif()
    endif()
  endif()
  message(STATUS "The path of OpenCV is ${OpenCV_DIR}.")
  if(ANDROID)
    if(WITH_ANDROID_OPENCV_STATIC)
      if(WITH_ANDROID_STATIC_LIB)
        # Only need the headers of opencv while using FastDeploy static lib.
        list(APPEND FASTDEPLOY_INCS ${OpenCV_DIR}/include)
      else()
        find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR})
        list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
        # For now, we still need to link OpenCV static libs.
        # Users may use some of opencv's apis, but they may
        # not have been compiled into fastdeploy.
        # list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS})
        list(APPEND FASTDEPLOY_LIBS opencv_core opencv_video opencv_highgui opencv_imgproc opencv_imgcodecs)
      endif()
    else()
      set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}/include)
      get_filename_component(OpenCV_NATIVE_DIR ${OpenCV_DIR} DIRECTORY)
      set(OpenCV_LIBS_DIR ${OpenCV_NATIVE_DIR}/libs)
      if(ANDROID_TOOLCHAIN MATCHES "clang")  # use opencv 4.x
        add_library(opencv_java4 STATIC IMPORTED GLOBAL)
        set_property(TARGET opencv_java4 PROPERTY IMPORTED_LOCATION ${OpenCV_LIBS_DIR}/${ANDROID_ABI}/libopencv_java4.so)
        list(APPEND FASTDEPLOY_LIBS opencv_java4)
      elseif(ANDROID_TOOLCHAIN MATCHES "gcc")  # use opencv 3.x
        add_library(opencv_java3 STATIC IMPORTED GLOBAL)
        set_property(TARGET opencv_java3 PROPERTY IMPORTED_LOCATION ${OpenCV_LIBS_DIR}/${ANDROID_ABI}/opencv_java3.so)
        list(APPEND FASTDEPLOY_LIBS opencv_java3)
      else()
        message(FATAL_ERROR "Only support clang/gcc toolchain, but found ${ANDROID_TOOLCHAIN}.")
      endif()
      list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
      message(STATUS "FASTDEPLOY_INCS: ${FASTDEPLOY_INCS}")
    endif()
  # Win/Linux/Mac
  else()
    find_package(OpenCV REQUIRED PATHS ${OpenCV_DIR} NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_INCS ${OpenCV_INCLUDE_DIRS})
    list(APPEND FASTDEPLOY_LIBS ${OpenCV_LIBS})
  endif()

  if(ENABLE_FLYCV)
    include_directories(${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/include)
    set(FLYCV_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/flycv/lib)
    if(ANDROID)
      if(NOT WITH_ANDROID_FLYCV_STATIC)
        add_library(flycv_shared STATIC IMPORTED GLOBAL)
        set_property(TARGET flycv_shared PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libflycv_shared.so)
        list(APPEND FASTDEPLOY_LIBS flycv_shared)
      else()
        # This code may be needed later. Therefore, I choose to
        # comment it rather than delete it. (TODO:qiuyanjun)
        # add_library(flycv_static STATIC IMPORTED GLOBAL)
        # add_library(flycv_png16 STATIC IMPORTED GLOBAL)
        # add_library(flycv_turbojpeg STATIC IMPORTED GLOBAL)
        # add_library(flycv_z STATIC IMPORTED GLOBAL)
        # set_property(TARGET flycv_static PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libflycv_static.a)
        # set_property(TARGET flycv_png16 PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libpng16.a)
        # set_property(TARGET flycv_turbojpeg PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libturbojpeg.a)
        # set_property(TARGET flycv_z PROPERTY IMPORTED_LOCATION ${FLYCV_LIB_DIR}/${ANDROID_ABI}/libz.a)
        # list(APPEND FASTDEPLOY_LIBS flycv_static)
        # list(APPEND FASTDEPLOY_LIBS flycv_png16)
        # list(APPEND FASTDEPLOY_LIBS flycv_turbojpeg)
        # list(APPEND FASTDEPLOY_LIBS flycv_z)
      endif()
    else()
      find_library(FLYCV_LIB flycv_shared ${FLYCV_LIB_DIR} NO_DEFAULT_PATH)
      list(APPEND FASTDEPLOY_LIBS ${FLYCV_LIB})
    endif()
  endif()

  if(ENABLE_CVCUDA)
    find_library(CVCUDA_LIB cvcuda ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/cvcuda/lib NO_DEFAULT_PATH)
    find_library(NVCV_TYPES_LIB nvcv_types ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/cvcuda/lib NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_LIBS ${CVCUDA_LIB} ${NVCV_TYPES_LIB})
    list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/cvcuda/include NO_DEFAULT_PATH)
    add_definitions(-DENABLE_CVCUDA)
  endif()

endif()

if (ENABLE_TEXT)
  if(ANDROID)
    if(NOT ANDROID_TOOLCHAIN MATCHES "clang")
      message(FATAL_ERROR "Currently, only support clang toolchain while cross compiling FastDeploy for Android with FastTokenizer, but found ${ANDROID_TOOLCHAIN}.")
    endif()
    add_library(core_tokenizers STATIC IMPORTED GLOBAL)
    set_property(TARGET core_tokenizers PROPERTY IMPORTED_LOCATION
                ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/fast_tokenizer/lib/${ANDROID_ABI}/libcore_tokenizers.so)
    list(APPEND FASTDEPLOY_LIBS core_tokenizers)
  else()
    # Add dependency libs later: Linux/Mac/Win/...
    find_library(FAST_TOKENIZER_LIB core_tokenizers ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/fast_tokenizer/lib NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_LIBS ${FAST_TOKENIZER_LIB})
  endif()
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/fast_tokenizer/include)
  list(APPEND FASTDEPLOY_INCS ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/fast_tokenizer/third_party/include)
endif()

if(ENABLE_PADDLE2ONNX)
  if(ANDROID)
    message(FATAL_ERROR "Not support fastdeploy-paddle2onnx APIs with Android now!")
  endif()
  if(NOT BUILD_PADDLE2ONNX)
    find_library(PADDLE2ONNX_LIB paddle2onnx  ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib NO_DEFAULT_PATH)
    list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
  endif()
endif()

if(WITH_KUNLUNXIN)
  list(APPEND FASTDEPLOY_LIBS -lpthread -lrt -ldl)
endif()

# log lib for Android
if(ANDROID)
  find_library(log-lib log)
  list(APPEND FASTDEPLOY_LIBS ${log-lib})
endif()

# Update CXX LINKER's FLAGS, reference: https://zhuanlan.zhihu.com/p/595527528
if(ANDROID AND (WITH_ANDROID_OPENCV_STATIC OR WITH_ANDROID_LITE_STATIC))
  set(COMMON_LINK_FLAGS_REL "-Wl,-s,--gc-sections,-exclude-libs,ALL")
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${COMMON_LINK_FLAGS_REL} -Wl,-allow-multiple-definition" CACHE INTERNAL "" FORCE)
  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${COMMON_LINK_FLAGS_REL} -Wl,-allow-multiple-definition" CACHE INTERNAL "" FORCE)
endif()

remove_duplicate_libraries(FASTDEPLOY_LIBS)

include(${CMAKE_CURRENT_LIST_DIR}/summary.cmake)
fastdeploy_summary()
message(STATUS "  DEPENDENCY_LIBS           : ${FASTDEPLOY_LIBS}")

if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.4.0")
    string(STRIP "${CMAKE_CXX_COMPILER_VERSION}" CMAKE_CXX_COMPILER_VERSION)
    message(FATAL_ERROR "[ERROR] FastDeploy require g++ version >= 5.4.0, but now your g++ version is ${CMAKE_CXX_COMPILER_VERSION}, this may cause failure! Use -DCMAKE_CXX_COMPILER to define path of your compiler.")
  endif()
endif()

function(install_fastdeploy_libraries DESTINATION_DIR)
  # No dynamic libs need to install while using
  # FastDeploy static lib.
  if(ANDROID AND WITH_ANDROID_STATIC_LIB)
    return()
  endif()
  set(DYN_LIB_SUFFIX "*.so*")
  if(WIN32)
    set(DYN_LIB_SUFFIX "*.dll")
  elseif(APPLE)
    set(DYN_LIB_SUFFIX "*.dylib*")
  endif()
  if(FastDeploy_DIR)
    set(DYN_SEARCH_DIR ${FastDeploy_DIR})
  elseif(FASTDEPLOY_INSTALL_DIR)
    set(DYN_SEARCH_DIR ${FASTDEPLOY_INSTALL_DIR})
  else()
    message(FATAL_ERROR "Please set FastDeploy_DIR/FASTDEPLOY_INSTALL_DIR before call install_fastdeploy_libraries.")
  endif()
  file(GLOB_RECURSE ALL_NEED_DYN_LIBS ${DYN_SEARCH_DIR}/lib/${DYN_LIB_SUFFIX})
  file(GLOB_RECURSE ALL_DEPS_DYN_LIBS ${DYN_SEARCH_DIR}/third_libs/${DYN_LIB_SUFFIX})

  if(ENABLE_VISION)
    # OpenCV
    if(ANDROID)
      file(GLOB_RECURSE ALL_OPENCV_DYN_LIBS ${OpenCV_NATIVE_DIR}/libs/${DYN_LIB_SUFFIX})
    else()
      file(GLOB_RECURSE ALL_OPENCV_DYN_LIBS ${OpenCV_DIR}/${DYN_LIB_SUFFIX})
    endif()
    list(REMOVE_ITEM ALL_DEPS_DYN_LIBS ${ALL_OPENCV_DYN_LIBS})

    if(WIN32)
      file(GLOB OPENCV_DYN_LIBS ${OpenCV_DIR}/x64/vc15/bin/${DYN_LIB_SUFFIX})
      file(INSTALL ${OPENCV_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
    elseif(ANDROID AND (NOT WITH_ANDROID_OPENCV_STATIC))
      file(GLOB OPENCV_DYN_LIBS ${OpenCV_NATIVE_DIR}/libs/${ANDROID_ABI}/${DYN_LIB_SUFFIX})
      file(INSTALL ${OPENCV_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
    else() # linux/mac
      file(GLOB OPENCV_DYN_LIBS ${OpenCV_DIR}/lib/${DYN_LIB_SUFFIX})
      file(INSTALL ${OPENCV_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
    endif()

    # FlyCV
    if(ENABLE_FLYCV)
      file(GLOB_RECURSE ALL_FLYCV_DYN_LIBS ${FLYCV_LIB_DIR}/${DYN_LIB_SUFFIX})
      list(REMOVE_ITEM ALL_DEPS_DYN_LIBS ${ALL_FLYCV_DYN_LIBS})
      if(ANDROID AND (NOT WITH_ANDROID_FLYCV_STATIC))
        file(INSTALL ${ALL_FLYCV_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
      endif()
    endif()
  endif()

  if(ENABLE_OPENVINO_BACKEND)
    # need plugins.xml for openvino backend
    set(OPENVINO_RUNTIME_BIN_DIR ${OPENVINO_DIR}/bin)
    file(GLOB OPENVINO_PLUGIN_XML ${OPENVINO_RUNTIME_BIN_DIR}/*.xml)
    file(INSTALL ${OPENVINO_PLUGIN_XML} DESTINATION ${DESTINATION_DIR})
  endif()

  # Install other libraries
  file(INSTALL ${ALL_NEED_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
  file(INSTALL ${ALL_DEPS_DYN_LIBS} DESTINATION ${DESTINATION_DIR})
endfunction()
